const Banner = require('../model/banner.model');
class BannerService {
async getBanner(bannername){
  const res = await Banner.findOne({where:{bannername}});
  return res;
}

  async bannerAdd(banner) {
    const res = await Banner.create(banner);
    return res.dataValues;
  }


  async bannerUpdate(banner) {
    const res = await Banner.update(banner, { where: { id: banner.id } });
    return res[0] > 0;
  }

  async bannerDelete(id) {
    const res = await Banner.destroy({ where: { id } });
    return res > 0;
  }

  async bannerDetail(id) {
    const res = await Banner.findByPk(id);
    return res ? res.dataValues : null;
  }

  async bannerList(page = 1, pageSize = 10, otherBannerOptions = {}) {
    const offset = (page - 1) * pageSize
    const options = {
      offset,
      limit: pageSize,
      order:[['id','DESC']]
    };
    if (otherBannerOptions.bannername != '' && otherBannerOptions.bannername != null && otherBannerOptions.bannername) {
      options.where = {
        bannername: otherBannerOptions.bannername // 根据 bannername 进行查询
      };
    }
    const res = await Banner.findAll(options);
    const total = await Banner.count();
    return {
      res,total
    };
  }
}
module.exports = new BannerService();